<template>
  <el-container>
    <el-header>
      <log-reg-header></log-reg-header>
    </el-header>
    <el-main>
      <div id="tips">
        <p>请输入用户名和密码</p>
      </div>
      <el-form
        :model="loginForm"
        :rules="loginFormRules"
        ref="loginFormRef"
        label-width="100px"
        class="demo-ruleForm"
        hide-required-asterisk
      >
        <!-- action="/api/login.do" -->
        <el-form-item label="用户名" prop="username" >
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密 码" prop="password">
          <el-input v-model="loginForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="success" class="login_btn" @click="login">登录</el-button>
          <a href="/register">没有账号？去注册</a>
        </el-form-item>
      </el-form>
    </el-main>
    <el-dialog title="提示" :visible.sync="loginDialogVisible" width="50%">
      <span>登录成功，点击确定进入个人中心</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="loginDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="toPersonal(loginForm.username)">确 定</el-button>
      </span>
    </el-dialog>
  </el-container>
</template>

<script>
import logRegHeader from "./components/login_reg/Header";

export default {
  data() {
    return {
      //登录表单绑定数据
      loginForm: {
        username: "admin",
        password: "1111"
      },
      // 登录表单验证对象
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      },
      // 控制登录确认框的显示隐藏
      loginDialogVisible:false
    };
  },
  methods: {
    //登录
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return;
        //这里后端只接受 表单形式的数据
        const res = await this.$http.post(
          "/login.do",
          this.$qs.stringify(this.loginForm)
        );
        console.log(res);
        if (res.status !== 200) return this.$message.error("请求错误");
        // true 不能加 引号！！！
        if (res.data.success !== true) return this.$message.error(res.data.msg);
        this.loginDialogVisible=true
      });
    },
    // 跳转到个人中心
    toPersonal(username){
      this.loginDialogVisible=false;
      //登录成功后将isLogin保存在 sessionStorage 中 并设置为true
      //个人中心 以及 投资等API接口需要在登录之后才能访问
      window.sessionStorage.setItem('isLogin','true')
      window.sessionStorage.setItem('username',this.loginForm.username)
      this.$router.push('/personal')
    }
  },
  components: {
    logRegHeader
  }
};
</script>

<style lang="less" scoped>
.el-main {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.el-form {
  width: 350px;
}
#tips {
  p {
    color: #888;
    font-size: 19px;
  }
}
.login_btn {
  width: 80px;
  position: relative;
  left: 20px;
  margin-right: 35px;
}
</style>